require 'proxy_pump/api'
require 'proxy_pump/daemonize'
module ProxyPump
  # JSON API server
  class Server

    include ProxyPump::Daemonize

    def host
      options[:host]
    end

    def port
      options[:port]
    end

    def start
      if daemonize?
        $stdout.puts <<-HEADER.strip_heredoc
        => Booting ProxyPump API Server
        => ProxyPump API server starting on http://#{host}:#{port}
        * Daemonizing...
        HEADER
        daemonize
      else
        $stdout.puts <<-HEADER.strip_heredoc
        => Booting ProxyPump API Server
        => ProxyPump API server starting on http://#{host}:#{port}
        * Use Ctrl-C to shutdown server
        HEADER
      end
      write_pidfile
      trap_signals
      redirect_io
      server_opts = {
        app: ProxyPump.application,
        Host: host,
        Port: port,
        daemonize: false
      }
      Rack::Server.start server_opts
    end

  end # Server
end # ProxyPump
